﻿Ext.define('app.prm.search.Project', {
    extend: 'Ext.panel.Panel',
    layout: 'border',
    border: false,
    initComponent: function () {
        var me = this;
        me.items = [me.getSearchGrid(), me.getSearchForm()];
        me.callParent(arguments);
    },
    getSearchGrid: function () {
        if (!this.searchGrid) {
            this.store = Ext.create("app.ux.PageInquiryStore", { url: '/api/services/app/search/GetProject', autoLoad: false });
            this.pagingBar = Ext.create({
                xtype: 'pagingtoolbar',
                store: this.store,   // same store GridPanel is using
                dock: 'bottom',
                displayInfo: true
            });
            this.searchGrid = new Ext.grid.Panel({
                title: '项目列表',
                viewConfig: {
                    enableTextSelection: true
                },
                border: false,
                multiColumnSort: true,
                forceFit: false,
                store: this.store,
                dockedItems: [this.pagingBar],
                region: 'center',
                columns: [{
                    text: "#", xtype: 'rownumberer', width: 50
                },
                    { header: localize('ProjectNo'), dataIndex: 'projectNo' },
                    { header: localize('ProjectName'), dataIndex: 'projectName' },
                    { header: localize('ProductNo'), dataIndex: 'productNo' },
                    { header: localize('Customer'), dataIndex: 'customer' },
                    { header: localize('CustomerCode'), dataIndex: 'customeCode' },
                    { header: '事业部', dataIndex: 'business' },
                    { header: localize('RdCenter'), dataIndex: 'rdCenter' },
                    { header: localize('Laboratory'), dataIndex: 'laboratory' },
                    { header: localize('PlanStartDate'), dataIndex: 'planStartDate' },
                    { header: localize('PlanEndDate'), dataIndex: 'planEndDate' },
                    { header: localize('RequiredCompleteDate'), dataIndex: 'targetEndDate' }
                ]
            });
        }
        return this.searchGrid;
    },
    getSearchForm: function () {
        var me = this;
        if (!this.searchForm) {
            this.searchForm = new Ext.form.Panel({
                title: '查询条件',
                defaultType: 'textfield',
                border: false,
                split: true,
                region: 'east',
                width: 300,
                defaults: { anchor: '95%' },
                fieldDefaults: {
                    labelWidth: 70,
                    labelAlign: "right",
                    flex: 1,
                    margin: 6
                },
                items: [ 
                    {
                        fieldLabel: '项目名称',
                        name: 'projectName'
                    },
                    {
                        fieldLabel: '项目编号',
                        name: 'projectCode'
                    },
                    {
                        fieldLabel: '产品号',
                        name: 'productNo'
                    },
                    {
                        fieldLabel: '客户',
                        name: 'customer'
                    },
                    {
                        fieldLabel: '客户编号',
                        name: 'customerCode'
                    },
                    me.getComboTree(),
                    {
                        xtype: 'panel',
                        border: false,
                        layout: {
                            type: 'hbox',
                            align: 'middle ',
                            pack: 'center'
                        },
                        items: [
                            {
                                xtype: 'button',
                                text: '查询',
                                width: 50,
                                handler: function () { me.search(); }
                            },
                            { border: false, width: 3 },
                            {
                                xtype: 'button',
                                text: '清空',
                                width: 50,
                                handler: function () { me.clearSearch(); }
                            }
                        ]
                    }
                ]
            });
        }
        return this.searchForm;
    },
    getComboTree: function () {
        if (!this.comboTree) {
            this.comboTree = Ext.create("app.ux.ComboTreeBox", {
                //id: 'cbo_tree',
                //anchor: '85%',
                fieldLabel: '所属',
                editable: false,
                store: new Ext.data.TreeStore({
                    root: {
                        expanded: true
                    },
                    autoLoad: true,
                    nodeParam: 'parentId',
                    // 根节点的参数值是0
                    defaultRootId: "0",
                    proxy: {
                        type: 'ajax',
                        reader: {
                            type: 'json',
                            rootProperty: function (node) {
                                return node.result || node.children;
                            }
                        },
                        url: '/api/services/app/laboratory/GetTree'
                    }
                })
            });
        }
        return this.comboTree;
    },
    search: function () {
        var me = this;
        var valJson = me.getSearchForm().getForm().getValues();
        var cbo_tree = me.getComboTree();
        var businessId = cbo_tree.BusinessId;
        var rdCenterId = cbo_tree.RdCenterId;
        var laboratoryId = cbo_tree.LaboratoryId;

        valJson.businessId = cbo_tree.BusinessId;
        valJson.rdCenterId = cbo_tree.RdCenterId;
        valJson.laboratoryId = cbo_tree.LaboratoryId;
        if (valJson.projectName == ""
            && valJson.projectCode == ""
            && valJson.productNo == ""
            && valJson.customer == ""
            && valJson.customerCode == ""
            && businessId == 0
            && rdCenterId == 0
            && laboratoryId == 0) {
            Ext.Msg.alert('系统提示', '至少输入一个查询条件');
            return false;
        }
        me.getSearchGrid().store.filter = valJson;
        me.pagingBar.moveFirst();
    },
    clearSearch: function () {
        var me = this;
        me.getSearchForm().getForm().reset();
        var cbo_tree = me.getComboTree();
        cbo_tree.businessId = 0;
        cbo_tree.rdCenterId = 0;
        cbo_tree.laboratoryId = 0;
    }
});